var g_MapAreaData={};
var g_MaxIDX=41
var g_MapCellIcon=
{
    0:"suitcase",1:"heart",2:"key",3:"location",4:"music",5:"notepad",6:"phone",7:"pie chart",8:"plug",9:"shopping cart"
    ,10:"smile",11:"sound",12:"tag",13:"target",14:"tools",15:"user_female",16:"user_male",17:"video",18:"star",19:"basket-empty"
    ,20:"basket-full",21:"chat-support",22:"checklist",23:"checkout",24:"contact",25:"checkout",26:"delivery",27:"downloads"
    ,28:"empty-cart",29:"full-cart",30:"gifts",31:"loaded-truck",32:"secure-payment",33:"shop-closed",34:"shop-open"
    ,35:"shopping cart",36:"shopping-bag",37:"gifts",38:"telephone",39:"wallet",40:"search"
}
var TiledMapData=
{
    getMapLineCount:function()
    {
        if(!this._MapLineCount)this._MapLineCount=100;
        return this._MapLineCount;
    },
    setMapLineCount:function(value)
    {
        this._MapLineCount=value;
        this._ResetMapData();
    },
    getMapRowCount:function()
    {
        if(!this._MapRowCount)this._MapRowCount=100;
        return this._MapRowCount;
    },
    setMapRowCount:function(value)
    {
        this._MapRowCount=value;
        this._ResetMapData();
    },
    getMapAreaLineCount:function()
    {
        if(!this._MapAreaLineCount)this._MapAreaLineCount=12;
        return this._MapAreaLineCount;
    },
    setMapAreaLineCount:function(value)
    {
        this._MapAreaLineCount=value;
        this._ResetMapData();
    },
    getMapAreaRowCount:function()
    {
        if(!this._MapAreaRowCount)this._MapAreaRowCount=20;
        return this._MapAreaRowCount;
    },
    setMapAreaRowCount:function(value)
    {
        this._MapAreaRowCount=value;
        this._ResetMapData();
    },
    getQueryDataSize()
    {
        if(!this._QueryDataSize)this._QueryDataSize=3;
        return this._QueryDataSize;
    },
    setQueryDataSize(value)
    {
        this._QueryDataSize=value;
    },
    getIconUrls()
    {
        var urls = new Array();
        for(var key in g_MapCellIcon)
        {
            urls.push("./prefab/TiledMap/img/"+g_MapCellIcon[key]);
        }
        return urls;
    },
    _ResetMapData:function()
    {
        g_MapAreaData={};
        for(var lMIndex=0;lMIndex<this.getMapLineCount();lMIndex++)
        {
            for(var rMIndex=0;rMIndex<this.getMapRowCount();rMIndex++)
            {
                if(!g_MapAreaData[lMIndex])g_MapAreaData[lMIndex]={};
                g_MapAreaData[lMIndex][rMIndex]=this._InitMapAreaData(lMIndex,rMIndex);
            }
        }
    },
    _InitMapAreaData:function(lLineIndex,lRowIndex)
    {
        var mData={};
        mData.MapID=lLineIndex*100000000+lRowIndex*1000000;
        mData.mLine=lLineIndex;
        mData.mRow=lRowIndex;
        mData.Data={};
        for(var lline=0;lline<this.getMapAreaLineCount();lline++)
        {
            for(var rRow=0;rRow<this.getMapAreaRowCount();rRow++)
            {
                if(!mData.Data[lline])mData.Data[lline]={};
                var key = Math.floor(Math.random()*g_MaxIDX);
                mData.Data[lline][rRow]=
                {
                    imgUrl:"./prefab/TiledMap/img/"+g_MapCellIcon[key],
                    index:lLineIndex*10000+lline*1000+lline*100+rRow,
                    TiledID:lLineIndex+"-"+lRowIndex+"-"+lline+"-"+rRow,
                    mpLine:lLineIndex,
                    mpRow:lRowIndex,
                    tLine:lline,
                    tRow:rRow,
                    SelectState:false,
                    Type:key
                }
            }
        }
        return mData;
    },
    SetSelectState:function(data)
    {
        if(!data)return ;
        if(!data.mpLine || !data.mpRow) return ;
        var areaData = g_MapAreaData[data.mpLine][data.mpRow];
        if(!areaData.Data[data.tLine] || !areaData.Data[data.tLine][data.tRow]) return ;
        areaData.Data[data.tLine][data.tRow]=data;
    },
    _QueryMapData:function(line,row,UpdateMapDataCallBack)
    {
        var Data={};
        Data.CenterLine = line;
        Data.CenterRow  = row;
        for(var l =0;l<this.getQueryDataSize();l++)
        {
            var qLine=line+(Math.floor(l-0.5*this.getQueryDataSize()));
            Data[l]={};
            for(var r=0;r<this.getQueryDataSize();r++)
            {
                var qRow=row+(Math.floor(r-0.5*this.getQueryDataSize()));
                Data[l][r]=g_MapAreaData[qLine][qRow];
            }
        }
        if(UpdateMapDataCallBack)UpdateMapDataCallBack(Data);
    },
    CheckCanMoveData:function(line,row)
    {
        var Data={};
        for(var l =0;l<this.getQueryDataSize();l++)
        {
            var qLine=line+(Math.floor(l-0.5*this.getQueryDataSize()));
            if(!g_MapAreaData[qLine])return false; 
            Data[l]={};
            for(var r=0;r<this.getQueryDataSize();r++)
            {
                var qRow=row+(Math.floor(r-0.5*this.getQueryDataSize()));
                if(!g_MapAreaData[qLine][qRow])return false; 
            }
        }
        return true;
    },
    QueryMapInitData:function(UpdateMapDataCallBack)
    {
        var line = Math.ceil(Math.random()*(this.getMapAreaLineCount()-Math.floor(this.getQueryDataSize())));
        var row = Math.ceil(Math.random()*(this.getMapAreaRowCount()-Math.floor(this.getQueryDataSize())));
        while(false == this.CheckCanMoveData(line,row))
        {
            line = Math.ceil(Math.random()*(this.getMapAreaLineCount()-Math.floor(this.getQueryDataSize())));
            row = Math.ceil(Math.random()*(this.getMapAreaRowCount()-Math.floor(this.getQueryDataSize())));
        }
        this._QueryMapData(line,row,UpdateMapDataCallBack);
    },
    QueryMapData:function(CurrentLine,CurrentRow,UpdateMapDataCallBack)
    {
        if(this.CheckCanMoveData(CurrentLine,CurrentRow))
        {
            this._QueryMapData(CurrentLine,CurrentRow,UpdateMapDataCallBack);
        }
    }
};
TiledMapData._ResetMapData();
module.exports=TiledMapData;
